package com.itheima.service.empl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.service.ShoppingCartService;
import com.itheima.utile.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    /**
     * 获取购物车的清单
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findAll() {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        return shoppingCartDao.selectList(qw);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        shoppingCart.setUserId(BaseContext.getThreadLocal());
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        if (dishId != null) {
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartDao.selectOne(qw);
        if (cart != null) {
            Integer number = cart.getNumber() + 1;
            cart.setNumber(number);
            shoppingCartDao.updateById(cart);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartDao.insert(shoppingCart);
            cart = shoppingCart;
        }
        return cart;
    }

    /**
     * 清空用户购物车
     *
     * @param
     * @return
     */
    @Override
    public int delete() {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        return shoppingCartDao.delete(qw);
    }

    /**
     * 删除购物车
     *
     * @param map
     * @return
     */
    @Override
    public ShoppingCart delete(Map<String, Long> map) {
        Long dishId = map.get("dishId");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        if (dishId != null) {
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            qw.eq(ShoppingCart::getSetmealId, map.get("setmealId"));
        }
        ShoppingCart shoppingCart = shoppingCartDao.selectOne(qw);
        Integer number = shoppingCart.getNumber();
        if (number > 1) {
            shoppingCart.setNumber(number-1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartDao.updateById(shoppingCart);
        }else {
            shoppingCartDao.delete(qw);
            shoppingCart=null;
        }
        return shoppingCart;
    }
}
